﻿Imports System.Data.SqlClient

Public Class frmTechnicalSupportManagement

    Sub FillTickets()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim StrCond, OasisComments, ClosedBy, ClosedOn As String

            'Check Claim Technical Status
            If Me.RActive.Checked = True Then
                StrCond += " and Status=N'Active'"

            ElseIf Me.RClosed.Checked = True Then
                StrCond += " and Status=N'Closed'"
            End If

            'Check Claim Financial Status
            If Me.RFilterModule.Checked = True Then
                StrCond += " and Module Like N'%" & Me.CombModules.Text & "'"
            End If

            Dim cmd As New SqlCommand("Select * From Support_Tickets Where ClientID=" & TechnicalSupportCompanyID & _
                                      StrCond & " Order By SNo", cnnSupport)
            Dim Reader As SqlDataReader
            Dim X As Integer

            Me.GridTickets.Rows.Clear()

            cnnSupport.Open()
            Reader = cmd.ExecuteReader
            While (Reader.Read)
                If Reader.Item("OasisComments") Is DBNull.Value Then
                    OasisComments = ""
                Else
                    OasisComments = Reader.Item("OasisComments")
                End If

                If Reader.Item("ClosedBy") Is DBNull.Value Then
                    ClosedBy = ""
                Else
                    ClosedBy = Reader.Item("ClosedBy")
                End If

                If Reader.Item("ClosedOn") Is DBNull.Value Then
                    ClosedOn = ""
                Else
                    ClosedOn = CDate(Reader.Item("ClosedOn")).ToString("yyyy/MM/dd HH:mm")
                End If


                Me.GridTickets.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("TicketNo"), Reader.Item("Status"), _
                                                      Reader.Item("Priority"), Reader.Item("Type"), Reader.Item("Module"), Reader.Item("ProblemDescription"), _
                                                      Reader.Item("SubmittedBy"), CDate(Reader.Item("SubmittedOn")).ToString("yyyy/MM/dd HH:mm"), _
                                                      OasisComments, ClosedBy, ClosedOn})
            End While
            cnnSupport.Close()

            Me.lblResult.Text = "Results found: " & Me.GridTickets.Rows.Count

            'Check Tickets Status
            For Each row As DataGridViewRow In Me.GridTickets.Rows
                If row.Cells(Me.DGColStatus.Index).Value = "Active" Then
                    row.Cells(Me.DGColStatus.Index).Style.BackColor = Color.Yellow

                ElseIf row.Cells(Me.DGColStatus.Index).Value = "Closed" Then
                    row.Cells(Me.DGColStatus.Index).Style.BackColor = Color.GreenYellow
                End If
            Next

            'Check Tickets Priority
            For Each row As DataGridViewRow In Me.GridTickets.Rows
                If row.Cells(Me.DGColPriority.Index).Value = "High" Then
                    row.Cells(Me.DGColPriority.Index).Style.BackColor = Color.Salmon

                ElseIf row.Cells(Me.DGColPriority.Index).Value = "Normal" Then
                    row.Cells(Me.DGColPriority.Index).Style.BackColor = Color.White

                ElseIf row.Cells(Me.DGColPriority.Index).Value = "Low" Then
                    row.Cells(Me.DGColPriority.Index).Style.BackColor = Color.CornflowerBlue
                End If
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnnSupport.State = ConnectionState.Open Then
                cnnSupport.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub btnRefresh_Click(sender As System.Object, e As System.EventArgs) Handles btnRefresh.Click
        FillTickets()
    End Sub

    Private Sub RFilterModule_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RFilterModule.CheckedChanged
        If Me.RFilterModule.Checked = True Then
            Me.CombModules.Enabled = True
        Else
            Me.CombModules.Enabled = False
        End If
    End Sub

    Private Sub frmTechnicalSupportManagement_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub btnNewTicket_Click(sender As System.Object, e As System.EventArgs) Handles btnNewTicket.Click
        Dim a As New frmNewTicket
        a.ShowDialog()

        FillTickets()
    End Sub
End Class